package com.examination.system.administrato;


import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int number = 30;
    Lock lock = new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"\t卖出第:"+(number--)+"\t还剩下:"+number);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}

/**
 * @author 寇淇芮
 * @Date 2019/10/12 22:58
 */
public class SaleTicketDemo01 {
    public static void main(String[] args) {
        List<String> stringList = new CopyOnWriteArrayList<>();
        Ticket ticket = new Ticket();
        new Thread(()->{for (int i = 0; i < 40; i++) ticket.sale(); },"张三").start();
        new Thread(()->{for (int i = 0; i < 40; i++) ticket.sale(); },"b").start();
        new Thread(()->{for (int i = 0; i < 40; i++) ticket.sale(); },"c").start();
        //不是马上
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        },"张三").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        },"b").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        },"c").start();
    }
}
